﻿

using Furion.DependencyInjection;
using Furion.DynamicApiController;
using IoTClient;
using IotClientService.Consts;
using IotContract.Dtos.Remotes.Response;
using IotContract.Enums;
using IotContract.Interfaces;
using IotContract.Models.Clients;
using IotContract.Models.Jobs.F29;
using IotContract.Models.Process;
using IotContract.Models.Process.F29;
using IotContract.Models.Webs;
using Mapster;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using XT.Common.Interfaces;
using XT.Common.Services;

namespace IotClientWeb.Controllers;
/// <summary>
/// 为第三方系统提供接口，如fms获取采集数据等
/// </summary>

[ApiDescriptionSettings(IotApiConst.ECS)]
[Route("ecs/api/")]
[AllowAnonymous]
public class EcsCallService:IDynamicApiController, ITransient
{
    private readonly ISignalRService _signalRService;
   
    private readonly IConnectDataService _connectDataService;
    public EcsCallService(IConnectDataService connectDataService, ISignalRService signalRService)
    {
        _connectDataService = connectDataService;
        _signalRService = signalRService;
    }
    /// <summary>
    /// 获取调度数据
    /// </summary>
    /// <param name="command"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "ProcessData", Tag = IotApiConst.Process)]
    public async Task<List<ProcessBaseData>> GetProcessDatas(ProcessProviderCommand command)
    {
        return await _connectDataService.GetProcessDatas(command);
    }

    /// <summary>
    /// 获取堆垛机调度数据
    /// </summary>
    /// <param name="command"></param>
    /// <returns></returns>
    [HttpGet]
    [ApiDescriptionSettings(Name = "GetProcessSc",Tag =IotApiConst.Process)]
    public async Task<List<F29ProcessScData>> GetProcessDatas(string nums)
    {
       var datas=  await _connectDataService.GetProcessDatas(new ProcessProviderCommand
        {
            Nums= nums,
            ProviderType=IotContract.Enums.ProcessProviderType.Sc
        });
        return datas.Adapt<List<F29ProcessScData>>();
    }

 
    /// <summary>
    /// 添加入库任务
    /// </summary>
    /// <param name="f29EnterJob"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "AddEnterJob",Tag =IotApiConst.Job)]
    public async Task<string> AddEnterJob(F29EnterJob f29EnterJob)
    {
        return await _connectDataService.AddProcessJob(new ProcessJobCommand
        {
            Job = f29EnterJob,
            ProviderType = IotContract.Enums.ProcessProviderType.EnterJob
        });
    }

    /// <summary>
    /// 添加出库任务
    /// </summary>
    /// <param name="f29EnterJob"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "AddOutJob",Tag =IotApiConst.Job)]
    public async Task<string> AddOutJob(FmsOutScReponse result)
    {      
        return await _connectDataService.AddProcessJob(new ProcessJobCommand
        {
            Job = new F29OutJob
            {
                EquipNum = result.StackerCode,
                ToTagNumber = result.ToTagNumber,
                JobID = result.TaskNumberId,
                FromX = result.FromR,
                FromY = result.FromC,
                FromZ = result.FromF,
                ToX = result.ToR,
                ToY = result.ToC,
                ToZ = result.ToF,
                ToTagNumber2 = result.ToTagNumber2,
                FromX2 = result.FromR2,
                FromY2 = result.FromC2,
                FromZ2 = result.FromF2,
                ToX2 = result.ToR2,
                ToY2 = result.ToC2,
                ToZ2 = result.ToF2,
                UsePoke = result.UsePoke,
                JobID2 = result.TaskNumberId2,
                Num = result.StackerCode,
                Tray = result.Tray,
                Tray2 = result.Tray2,
                RightTray= result.rTray,
                RightTray2=result.rTray2

            },
            ProviderType = IotContract.Enums.ProcessProviderType.OutJob
        });
    }

    /// <summary>
    /// 添加移库任务
    /// </summary>
    /// <param name="f29EnterJob"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "AddMoveJob", Tag = IotApiConst.Job)]
    public async Task<string> AddMoveJob(F29MoveJob f29MoveJob)
    {
        return await _connectDataService.AddProcessJob(new ProcessJobCommand
        {
            Job = f29MoveJob,
            ProviderType = IotContract.Enums.ProcessProviderType.MoveJob
        });
    }

    /// <summary>
    /// 添加充放电任务
    /// </summary>
    /// <param name="f29EnterJob"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "AddFmtJob",Tag =IotApiConst.Job)]
    public async Task<string> AddFmtJob(F29FmtJob f29FmtJob)
    {
        return await _connectDataService.AddProcessJob(new ProcessJobCommand
        {
            Job = f29FmtJob,
            ProviderType = IotContract.Enums.ProcessProviderType.OutJob
        });
    }
    /// <summary>
    /// 对设备进行写入、断开等控制,数据写入请调用 ControlHelp查看信息
    /// </summary>
    /// <param name="control"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name ="ControlEquip",Tag =IotApiConst.Control)]
    public async Task<string> ControlEquip(WebControlModel control)
    {
       return await _connectDataService.AddWebControl(control);
    }
    /// <summary>
    /// 获取设备写入帮助信息
    /// </summary>
    /// <returns></returns>
    [HttpGet]
    [ApiDescriptionSettings(Name = "ControlHelp", Tag = IotApiConst.Control)]
    public async Task<List<string>> ControlHelp()
    {


        var datas = new List<string>();
        // 扫码枪
        datas.Add(
            "扫码枪：请求扫码，Value=100"
        );

        datas.AddRange(new List<string>
        {
            "充放电：作业再开始， Value=133*E (从停止的站点开始连续作业)   Value=133*P（停止工程再作业(Recipe配方邀请))",
            "充放电：停止作业， Value=129*E (现工程作业结束)   Value=129*P（现工程暂停)",
        });

        return datas;
    }
    /// <summary>
    /// 触发弹窗
    /// </summary>
    /// <param name="type">0 log  1 warn</param>
    /// <param name="message"></param>
    /// <returns></returns>
    [HttpGet]
    [ApiDescriptionSettings(Name = "AvaloniaAlert", Tag = IotApiConst.Test)]
    public async Task AvaloniaAlert(int type,string message)
    {
        if (type == 1)
        {


            _signalRService.WriteMessage(new XT.Common.Models.SignalR.InformModel
            {
                Message = message,
                Sender = IotApiConst.API,
            });
        }
        else
        {
            _signalRService.WriteLog(new XT.Common.Models.SignalR.RemoteLog
            {
                Content = message,
                IsToRemote= true,
            });
        }


    }








}
